package com.ovopark.firecar.activity

import android.app.Dialog
import android.os.Handler
import android.text.Editable
import android.text.InputType
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.*
import androidx.core.content.ContextCompat
import cn.yhq.dialog.core.DialogBuilder
import cn.yhq.dialog.core.IDialog
import com.ovopark.firecar.Contents
import com.ovopark.firecar.R
import com.ovopark.firecar.bean.UserBean
import com.ovopark.firecar.manager.DataManager
import com.ovopark.firecar.manager.UserCacheManager
import com.ovopark.firecar.presenter.LoginPresenter
import com.ovopark.firecar.utils.SharedPreferencesUtils
import com.ovopark.firecar.utils.StringUtils
import com.ovopark.firecar.view.ILoginView

class LoginActivity : BaseActivity() {
    lateinit var etUsername: EditText
    lateinit var tvUsername: TextView
    lateinit var etPassword: EditText
    lateinit var tvPassword: TextView
    lateinit var spServiceChoose: TextView
    lateinit var ivSetting: ImageView
    lateinit var radNotConfirm: ImageView
    lateinit var tvLogin: TextView
    lateinit var ivPasswordAppear: ImageView
    lateinit var tvInputError: TextView
    var loginState: Boolean = false
    var userNameAndPasswordState = false
    var rembered: Boolean = false
    lateinit var presenter: LoginPresenter
    override fun initView() {
        super.initView()
        setContentView(R.layout.activity_login)
        etUsername = findViewById(R.id.et_username)
        tvUsername = findViewById(R.id.tv_username)
        etPassword = findViewById(R.id.et_password)
        tvPassword = findViewById(R.id.tv_password)
        spServiceChoose = findViewById(R.id.sp_service_choose)
        ivSetting = findViewById(R.id.iv_setting)
        radNotConfirm = findViewById(R.id.rad_not_confirm)
        tvLogin = findViewById(R.id.tv_login)
        tvInputError = findViewById(R.id.tv_input_error)
        ivPasswordAppear = findViewById(R.id.iv_password_appear)
        rembered = SharedPreferencesUtils.getInstance(
            sharePreferenceName
        )!!.getParam(this, rememberState, false) as Boolean
        if (rembered) {
            exchangeRembered()
            if (!StringUtils.isBlank(
                    SharedPreferencesUtils.getInstance(
                        sharePreferenceName
                    )!!.getParam(this, username, "") as String
                )
            ) {
                etUsername.text = Editable.Factory.getInstance().newEditable(
                    SharedPreferencesUtils.getInstance(
                        sharePreferenceName
                    )!!.getParam(this, username, "") as String
                )
            }
            if (!StringUtils.isBlank(
                    SharedPreferencesUtils.getInstance(
                        sharePreferenceName
                    )!!.getParam(this, password, "") as String
                )
            ) {
                etPassword.text = Editable.Factory.getInstance().newEditable(
                    SharedPreferencesUtils.getInstance(
                        sharePreferenceName
                    )!!.getParam(this, password, "") as String
                )
            }
        }
        initPresenter()
        ivPasswordAppear.setOnClickListener {
            loginState = !loginState
            exchangePasswordDisappear()
        }
        radNotConfirm.setOnClickListener {
            rembered = !rembered
            exchangeRembered()
        }

        tvLogin.setOnClickListener {
            SharedPreferencesUtils.getInstance(sharePreferenceName)!!.setParam(
                this,
                username, etUsername.text.trim().toString()
            )
            SharedPreferencesUtils.getInstance(sharePreferenceName)!!.setParam(
                this,
                password, etPassword.text.trim().toString()
            )
            loadingDialog.show()
            SharedPreferencesUtils.getInstance(sharePreferenceName)!!.setParam(
                this,
                rememberState, rembered
            )
            presenter.Login(
                etUsername.text.trim().toString(),
                etPassword.text.trim().toString(),
                "0"
            )
        }
        ivSetting.setOnClickListener {
            readyGo(this, AddServiceActivity::class.java)
        }
        ivSetting.setOnLongClickListener {
            resertServiceUrl()
            Toast.makeText(this,"服务器初始化成功",Toast.LENGTH_SHORT).show()
            true
        }
    }

    override fun onResume() {
        super.onResume()
        initViewData()
    }

    fun initViewData() {
        if (!StringUtils.isBlank(
                SharedPreferencesUtils
                    .getInstance(AddServiceActivity.sharePreferenceName)!!
                    .getParam(this, AddServiceActivity.name, "") as String
            )
        ) {
            spServiceChoose.text = SharedPreferencesUtils
                .getInstance(AddServiceActivity.sharePreferenceName)!!
                .getParam(this, AddServiceActivity.name, "") as String
            var url : String = "http://${SharedPreferencesUtils.getInstance(AddServiceActivity.sharePreferenceName)!!.
            getParam(this,AddServiceActivity.ip,"218.4.38.130")}:${SharedPreferencesUtils.getInstance(AddServiceActivity.sharePreferenceName)!!.
            getParam(this,AddServiceActivity.port,"9099")}"
            Log.i("url","url: "+url)
            DataManager.getInstance().setBaseUrl(url)
        } else {
            spServiceChoose.text = "默认服务器"
            spServiceChoose.setTextColor(ContextCompat.getColor(this, R.color.color_7F7F7F))
        }
    }

    fun initPresenter() {
        presenter = LoginPresenter(this, object : ILoginView {
            override fun loginSuccess(user: UserBean) {
                loadingDialog.dismiss()
                UserCacheManager.getInstance()?.setUsers(user)
                readyGo(this@LoginActivity, ReadcardActivity::class.java)
            }

            override fun loginFailed(mes: String?) {
                Toast.makeText(this@LoginActivity, mes, Toast.LENGTH_SHORT).show()
                loadingDialog.dismiss()
            }

        })
    }


    fun exchangeUserNameAndPasswordState() {
        if (userNameAndPasswordState) {
            tvInputError.visibility = View.VISIBLE
            tvUsername.setTextColor(ContextCompat.getColor(this, R.color.red_f13642))
            tvPassword.setTextColor(ContextCompat.getColor(this, R.color.red_f13642))
        } else {
            tvInputError.visibility = View.INVISIBLE
            tvUsername.setTextColor(ContextCompat.getColor(this, R.color.black))
            tvPassword.setTextColor(ContextCompat.getColor(this, R.color.black))
        }

    }

    fun exchangeRembered() {
        if (rembered) {
            radNotConfirm.background = ContextCompat.getDrawable(this, R.mipmap.iconselected)
        } else {
            radNotConfirm.background = ContextCompat.getDrawable(this, R.mipmap.radionotconfirm)

        }
    }

    fun exchangePasswordDisappear() {
        if (loginState) {
            ivPasswordAppear.background = ContextCompat.getDrawable(this, R.mipmap.iconappear)
            etPassword.inputType =
                InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            ivPasswordAppear.background = ContextCompat.getDrawable(this, R.mipmap.unappear)
            etPassword.inputType =
                InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
    }

    fun resertServiceUrl(){
        DataManager.getInstance().setBaseUrl("http://218.4.38.130:9099")
        SharedPreferencesUtils.getInstance(AddServiceActivity.sharePreferenceName)!!.setParam(this,
            AddServiceActivity.name,"默认服务器")
        SharedPreferencesUtils.getInstance(AddServiceActivity.sharePreferenceName)!!.setParam(this,
            AddServiceActivity.ip,"218.4.38.130")
        SharedPreferencesUtils.getInstance(AddServiceActivity.sharePreferenceName)!!.setParam(this,
            AddServiceActivity.port,"9099")
        spServiceChoose.text = "默认服务器"
    }


    companion object {
        val sharePreferenceName: String = "login"
        val username: String = "username"
        val password: String = "password"
        val rememberState: String = "rememberState"
    }


}